﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;

[CustomEditor(typeof(APController))]
public class APControllerEditor : Editor
{
    int selectedLayer = 0;
    [SerializeField]
    string m_ChosenTemplateName = "";
    bool showWaveTemplateOption = true;
    bool showWaveOption = true;

    bool forceRefresh = false;

    string[] displayTemplateNames = new string[0];
    int[] displayIndex = new int[0];
    public override void OnInspectorGUI()
    {
        //EditorUtility.SetDirty(target);
        #region 行动力属性相关
        APController myTarget = (APController)target;
        myTarget.uiText = EditorGUILayout.ObjectField("绑定的UIText对象", myTarget.uiText, typeof(Text), true) as Text;
        myTarget.APImage= EditorGUILayout.ObjectField("绑定的APImage对象", myTarget.APImage, typeof(Image), true) as Image;
        myTarget.basicDelayBetweenUnit = EditorGUILayout.FloatField("基准单位生成间隔", myTarget.basicDelayBetweenUnit);
        myTarget.playerWalkTimePerAP = EditorGUILayout.FloatField("单位AP可供Player走动时间", myTarget.playerWalkTimePerAP);
        myTarget.threshold = EditorGUILayout.FloatField("AP阈值", myTarget.threshold);
        #endregion

        EditorGUILayout.BeginHorizontal();
        showWaveTemplateOption = EditorGUILayout.Foldout(showWaveTemplateOption, "批次模版");
        if(GUILayout.Button("添加模版"))
        {
            myTarget.waveTempletes.Add(new APController.WaveTemplate());
        }
        EditorGUILayout.EndHorizontal();

        //如果点击下拉菜单
        if (showWaveTemplateOption)
        {
            for(int i = 0; i < myTarget.waveTempletes.Count; ++i)
            {
                //EditorGUILayout.LabelField(myTarget.waveTempletes[i].templateName);
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.PrefixLabel("模版名");
                myTarget.waveTempletes[i].templateName = EditorGUILayout.TextField(myTarget.waveTempletes[i].templateName);
                if (GUILayout.Button("删除"))
                {
                    myTarget.waveTempletes.RemoveAt(i);
                    EditorUtility.SetDirty(target);
                    //删除后进入下一帧重新渲染
                    return;
                }
                EditorGUILayout.EndHorizontal();


                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("兵种（后期改成枚举表示）", GUILayout.Width(50), GUILayout.ExpandWidth(true));
                EditorGUILayout.LabelField("次数", GUILayout.Width(50), GUILayout.ExpandWidth(true));
                EditorGUILayout.LabelField("延迟", GUILayout.Width(50), GUILayout.ExpandWidth(true));
                EditorGUILayout.EndHorizontal();

                for(int j = 0; j < myTarget.waveTempletes[i].waveUnits.Count; ++j)
                {
                    EditorGUILayout.BeginHorizontal();
                    myTarget.waveTempletes[i].waveUnits[j].soldierIndex = EditorGUILayout.IntField(myTarget.waveTempletes[i].waveUnits[j].soldierIndex);
                    myTarget.waveTempletes[i].waveUnits[j].unitRepeatTimes = EditorGUILayout.IntField(myTarget.waveTempletes[i].waveUnits[j].unitRepeatTimes);
                    myTarget.waveTempletes[i].waveUnits[j].unitDelaySecond = EditorGUILayout.FloatField(myTarget.waveTempletes[i].waveUnits[j].unitDelaySecond);
                    if (GUILayout.Button("x"))
                    {
                        myTarget.waveTempletes[i].waveUnits.RemoveAt(j);
                        forceRefresh = true;
                        EditorUtility.SetDirty(target);
                        return;
                    }
                    EditorGUILayout.EndHorizontal();
                }
                if (GUILayout.Button("+"))
                {
                    myTarget.waveTempletes[i].waveUnits.Add(new APController.WaveUnit());
                    forceRefresh = true;
                    EditorUtility.SetDirty(target);
                    return;
                }

                EditorGUILayout.Space();
                EditorGUILayout.Space();
            }
        }


        showWaveOption = EditorGUILayout.Foldout(showWaveOption, "批次设置");
        //当改变时才重新分配内存以优化性能
        if(displayTemplateNames.Length != myTarget.waveTempletes.Count)
        {
            displayTemplateNames = new string[myTarget.waveTempletes.Count];
        }
        if(displayIndex.Length != myTarget.waves.Count)
        {
            displayIndex = new int[myTarget.waves.Count];
            for(int i = 0; i < myTarget.waves.Count; ++i)
            {
                for(int j = 0; j < myTarget.waveTempletes.Count; ++j)
                {
                    if (myTarget.waveTempletes[j].templateName == myTarget.waves[i].templateName)
                    {
                        displayIndex[i] = j;
                        break;
                    }
                }
            }
        }
        for (int i = 0; i < myTarget.waveTempletes.Count; ++i)
        {
            displayTemplateNames[i] = myTarget.waveTempletes[i].templateName;
        }
        if (showWaveOption)
        {
            for(int i = 0; i < myTarget.waves.Count; ++i)
            {

                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("批次 " + i.ToString(), GUILayout.Width(50), GUILayout.ExpandWidth(true));
                EditorGUILayout.Space();
                if(GUILayout.Button("删除"))
                {
                    myTarget.waves.RemoveAt(i);
                    EditorUtility.SetDirty(target);
                    return;
                }
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("模版", GUILayout.Width(50),GUILayout.ExpandWidth(true));
                //每个wave有一个index！！！！
                displayIndex[i] = EditorGUILayout.Popup(displayIndex[i], displayTemplateNames);
                myTarget.waves[i].templateName = displayTemplateNames[displayIndex[i]];
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("重复次数", GUILayout.Width(50), GUILayout.ExpandWidth(true));
                myTarget.waves[i].waveRepeatTimes = EditorGUILayout.IntField(myTarget.waves[i].waveRepeatTimes);
                EditorGUILayout.EndHorizontal();
                /*EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("延迟秒数", GUILayout.Width(50), GUILayout.ExpandWidth(true));
                myTarget.waves[i]. = EditorGUILayout.TextField("");
                EditorGUILayout.EndHorizontal();*/
                EditorGUILayout.Space();
            }

            if (GUILayout.Button("添加批次"))
            {
                myTarget.waves.Add(new APController.Wave());
                EditorUtility.SetDirty(target);
                return;
            }

        }
        /*if(GUILayout.Button("保存"))
        {
            EditorUtility.SetDirty(target);

        }*/
        if (GUI.changed)
        {
            EditorUtility.SetDirty(target);
            Undo.RecordObject(myTarget, "obj change");
            //Debug.Log("刷新面板");
        }
        /*index = EditorGUILayout.Popup(index, new string[] { "111", "222" });
        if (EditorGUILayout.DropdownButton(new GUIContent(m_ChosenTemplateName), FocusType.Keyboard))
        {
            Debug.Log("click");
            GenericMenu menu = new GenericMenu();
            string name1 = "aaa";
            menu.AddItem(new GUIContent(name1), m_ChosenTemplateName.Equals(name1), OnWaveTemplateSelected, name1);
            string name2 = "bbb";
            menu.AddItem(new GUIContent(name2), m_ChosenTemplateName.Equals(name2), OnWaveTemplateSelected, name2);
            menu.AddItem(new GUIContent("添加Template..."), false, OnWaveTemplateSelected, name2);
            menu.ShowAsContext();
        }*/


    }

    void OnWaveTemplateSelected(object templateName)
    {
        //Debug.Log(templateName);
        m_ChosenTemplateName = (string)templateName;
    }

}
